﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using FeiraGame_XNA.src.core;
using FeiraGame_XNA.src.core.gameobjects;

namespace FeiraGame_XNA.src.core.gameobjects
{
    public class GameObject : InterfaceObject
    {
        #region Variables ( Precisa Arrumar )
        public enum animIndexes { Attack, Jump, Walk, Damage, Idle };
        public enum Elements { NONE, FIRE, WATHER, EARTH, ICE, DARK, LIGHT };
        public int Life { get; protected set; }
        public Elements Element { get; protected set; }
        
        public string Name { get; protected set; }
        public int Attack { get; protected set; }
        public float Width { get; protected set; }
        public float Height { get; protected set; }

        /// <summary>
        ///Estrutura criada para guardar as informações do Objeto. Struct Create for alocate all informations of Descriptions.
        /// </summary>
        public struct DescriptionObject
        {
            public Texture2D BG { get; set; }
            public string Text { get;  set; }
            public string Type { get; set; }
            public string BaseText { get; set; }
            public Vector2 Position { get;  set; }
            public SpriteFont Font { get; set; }
            public Color color { get; set; }
            public float ScaleFont { get; set; }
            public Vector2 ScaleBG { get; set; }

        }
        public DescriptionObject Description;
        #endregion

        /// <summary>
        /// Construtor
        /// </summary>
        /// <param name="texture">Imagem</param>
        /// <param name="BG">Fundo da Descrição</param>
        #region Constructors
        public GameObject(Texture2D texture,Texture2D BG = null):
            base(texture)
        {
            this.isAlive = true;
            this.Description.BG = Tools.Content.Load<Texture2D>("BGBase");
            this.Description.Position = Position;
            this.Description.Font = Tools.Content.Load<SpriteFont>("Description");
            this.Description.color = Color.White;
            
        }
        #endregion

        public override void Initialize()
        {
            base.Initialize();
            //this.Description.ScaleFont = this.Scale;
        }

        /// <summary>
        /// Função Criada para lembrar que todo objeto tem q ter as animações não importa se elas tem parte gráfica ou não. Ou seja todo objeto vai ter esse conjunto de animações dentro dele.
        /// </summary>
        /// <param name="attack"></param>
        /// <param name="jump"></param>
        /// <param name="walk"></param>
        /// <param name="damage"></param>
        /// <param name="idle"></param>
        #region ControlOfAnimations
        public virtual void controlOfAnimations(Animation attack, Animation jump, Animation walk, Animation damage, Animation idle)
        {
            if(this.animations == null)
               this.animations = new List<Animation>();
            this.animations.Add(attack);
            this.animations.Add(jump);
            this.animations.Add(walk);
            this.animations.Add(damage);
            this.animations.Add(idle);
            this.setAnimationIndex((int)animIndexes.Idle);
            this.ColisionBox = new Rectangle((int)position.X, (int)position.Y, getAnimation().Width, getAnimation().Height);
        }
        #endregion

        /// <summary>
        /// Basic Update With Move.
        /// </summary>
        #region BasicUpdate With Move
        public override void Update()
        {
            

            if (CheckMove(new Vector2(this.Width,this.Height)))
                this.Position += Speed;

            Tools.Trace(TileMap.GetTileAtPixel(this.Position + Speed));
            base.Update();
        }
        #endregion

        /// <summary>
        /// Draw que chama o pai e desenha as descrições
        /// </summary>
        /// <param name="gameTime"></param>
        /// <param name="spriteBatch"></param>
        /// <param name="animating"></param>
        #region Draw With Descriptions
        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch, bool animating)
        {
            base.Draw(gameTime, spriteBatch, animating);
            this.Height = this.Texture.Height * Scale;
            this.Width = this.Texture.Width * Scale;
            if (Inputs.MouseOver(this))
            {
                this.Description.Position = new Vector2(this.Position.X, this.Position.Y + (this.Height));
                this.Description.BaseText = "Type: "+ Description.Type +"\n" + "Name:" + this.Name + "\n" + "Description: " + Description.Text + "\n" + "Life: " + this.Life.ToString();
                Rectangle Source = new Rectangle((int)Description.Position.X, (int)Description.Position.Y, Description.BG.Width, Description.BG.Height);
                spriteBatch.Draw(Description.BG, Description.Position, Source, Description.color, 0f, Vector2.Zero, Description.ScaleBG, SpriteEffects.None, 1f);
                spriteBatch.DrawString(Description.Font,Description.BaseText, Description.Position, Description.color,0.0f,Vector2.Zero,Description.ScaleFont,SpriteEffects.None,1f);

            }
        }
        #endregion

        public virtual bool CheckMove(Vector2 Speed)
        {
            //if(TileMap.GetSquareAtPixel(this.Position + Speed))
            if (TileMap.GetTileAtPixel(this.Position + Speed) != 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

    }
}
